package com.liuxingzhiwu.ruijishangcheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.entity.Employee;
import com.liuxingzhiwu.ruijishangcheng.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登入
     * @param request
     * @param employee   前端返回的是json数据类型，用@RequestBod注解将json数据转为实体类对象
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if(emp==null){
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //5、查看员工状态，如果以为禁用，则返回员工已禁用结果
        if(emp.getStatus() == 0){
            return R.error("员工已禁用");
        }

        //6、登录成功，将员工id存入Session并返回登录成功的结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }


    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session中保存的当前员工的id
        request.getSession().removeAttribute("employee");
        return R.success("已退出登录");
    }


    /**
     * 添加员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){

        //1、利用md5算法，设置员工初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //2、设置员工创建时间
        employee.setCreateTime(LocalDateTime.now());
        //3、设置员工修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //4、设置创建者id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        //5、设置更新者id
        employee.setUpdateUser(empId);

        Boolean ret = employeeService.save(employee);

        if(ret){
            return R.success("添加成功");
        }

        return R.error("添加失败");
    }


    /**
     * 搜索，员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();

        //如果查询条件不为空，加入查询条件,StringUtils.isNotEmpty   mybatis-plus包下
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //分页构造器
        Page pageInfo = new Page<>(page,pageSize);
        employeeService.page(pageInfo,queryWrapper);  //可以不用返回Page，已经封装在Page里面了

        return R.success(pageInfo);
    }

    /**
     * 回显要修改的员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEditEmployee(@PathVariable("id") Long id){

        //按主键id查找员工信息
        Employee employee = employeeService.getById(id);

        return R.success(employee);
    }

    /**
     * 更新员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateEmployee(HttpServletRequest request,@RequestBody Employee employee){

        //设置更新条件
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Employee::getId,employee.getId());

        //设置更新时间
        employee.setUpdateTime(LocalDateTime.now());

        //设置更新者id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);

        Boolean ret = employeeService.update(employee,updateWrapper);
        if(ret)
        {
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }


}
